﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevTeam;

using System;
using System.Collections.Generic;

namespace ConfExpert.Forms.Depo
{
    public partial class Stler : XForm
    {
        public Stler()
        {
            InitializeComponent();
        }

        Dictionary<int, double[]> dbFeatures = new Dictionary<int, double[]>();
        private void Stler_Load(object sender, EventArgs e)
        {
            //FindSimilar();
            
            gvArtikel.Init("GetST2");
            gvArtikel.AddDetailGrid(gridView2, "STId");

            gvArtikel.Columns["STId"].ColumnEdit = RI(Rp.V_Kumas);
            GridColumn colPhoto = gvArtikel.Columns["ScST"];
            colPhoto.ColumnEdit = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
            (colPhoto.ColumnEdit as RepositoryItemPictureEdit).SizeMode = PictureSizeMode.Clip;
            (colPhoto.ColumnEdit as RepositoryItemPictureEdit).NullText = " ";

            btnMark.ItemClick += btnMark_ItemClick;
            gvArtikel.FormatAsNumber(1, "QtyST", "Qty");
            gvArtikel.AddSummary("QtyST", "Qty");

            gvArtikel.BestFitColumns();
            gridView2.BestFitColumns();

            //checkButton1.CheckedChanged += checkButton1_CheckedChanged;
            //checkButton1.Checked = true;
        }

        //void checkButton1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        //{
        //    gvArtikel.ActiveFilterString = checkButton1.Checked ? "QtyST>0" : string.Empty;
        //    gvArtikel.BestFitColumns();
        //}

        void btnMark_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (DetailTwins tw = new DetailTwins(intParse(gvArtikel.GetFocusedRowCellValue("STlerId"))))
                tw.ShowDialog();

            gvArtikel.RefreshDataSource("GetST2");
        }
        /*
        private void FindSimilar()
        {
            //CultureInfo ciEn = new System.Globalization.CultureInfo("en-US");
            //DataTable dtFeature = Session.DataManager.ExecuteDataTableByQuery("select * from productPhotoFeature");
            //foreach (DataRow dr in dtFeature.Rows)
            //{
            //    int productId = (int)dr["ProductId"];
            //    string features = dr["Feature"].ToString();
            //    List<double> doubles = new List<double>();
            //    foreach (string feature in features.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            //        doubles.Add(double.Parse(feature, ciEn));

            //    dbFeatures.Add(productId, doubles.ToArray());
            //}

            DataTable dtSt = Session.DataManager.ExecuteDataTable("GetST");
            foreach (DataRow dr in dtSt.Rows)
            {
                int productID = intParse(dr["ProductId"]);
                if (productID > 0)
                {
                    //List<int> result = GetSimilarsTopN(productID, 2);
                    //if (result.Count > 1)
                    //    dr["P2Id"] = result[1];


                    List<int> result = GetSimilarsTopN(productID, 8);
                    if (result.Count > 1)
                        if (!AppSession.Lookups.V_Kumas.FindByProductId(result[1]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[1];
                        else if (!AppSession.Lookups.V_Kumas.FindByProductId(result[2]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[2];
                        else if (!AppSession.Lookups.V_Kumas.FindByProductId(result[3]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[3];
                        else if (!AppSession.Lookups.V_Kumas.FindByProductId(result[4]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[4];
                        else if (!AppSession.Lookups.V_Kumas.FindByProductId(result[5]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[5];
                        else if (!AppSession.Lookups.V_Kumas.FindByProductId(result[6]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[6];
                        else if (!AppSession.Lookups.V_Kumas.FindByProductId(result[7]).Name.StartsWith("ST0"))
                            dr["P2Id"] = result[7];
                }
            }

            gvArtikel.Init(dtSt);
            gvArtikel.BestFitColumns();
        }
        */

    }
}
